var $;
var layer;
var table;
var form;

    layui.use(['layer', 'table', 'form'], function () {
        layer = layui.layer;
        $ = layui.jquery;
        table = layui.table;
        form = layui.form;

        getsel();//文章数据
        getuaer();//当前用户数据
        var limi=1;
        getcomment(limi);//评论区初始化

        //初始化数据
        function getsel(){
            var id=$("#mcId").val();
            if(id){
                $.post("/tea/selMicroCircle", {id:id}, function (res) {
                    if (res.code>-1) {
                        $("#UserName").text(res.obj.userName);
                        $(".Title").text(res.obj.articleTitle);
                        $("#className").text(res.obj.className);
                        $("#createDate").text(settime(res.obj.createDate));
                        $("#userAccount").text(res.obj.userAccount);
                        $("#content").text(res.obj.articleContent);
                        $("#articleHits").text(res.obj.articleHits);
                        $("#articleLike").text(res.obj.articleLike);
                        $("#collectNum").text(res.obj.collectNum);
                        $("#articleShare").text(res.obj.articleShare);
                    } else {
                        layer.msg("数据加载失败", {icon: 2});
                    }
                });
            }else{
                layer.msg("数据加载异常", {icon: 2}, function(){
                    var layer_4=parent.layer_4;
                    layer_4.close(layer_4.index);
                });
            }
        };

        //当前登录人的信息
        function getuaer() {
            $.post("/login/selUserContent", {}, function (res) {
                $("#UserId").val(res.obj.userId);
                $("#sendUserName").val(res.obj.account);
                $("#sendUserAccount").val(res.obj.phoneNumber);
                if(res.obj.avatarUrl){
                    $('#layui_i1').css("display","none");
                    $("#imgId").attr('src',res.obj.avatarUrl);
                }
            })
        };

        var liminum=0;
        //评论区
        function getcomment(num) {
            var id=$("#mcId").val();
            var com="";
            $.post("/tea/selComment", {"id":id,"limi":num}, function (res) {
                if (res.code>-1) {
                    $('#commentNum').html(res.obj.length);
                    if(res.obj.length!=liminum){
                        if(res.obj.length<num){
                            $('#nocomDiv').css("display","block");
                            $('#adiv').css("display","none");
                        }
                        liminum=res.obj.length;
                        for(var i=0;i<res.obj.length;i++){
                            if(res.obj[i].userImg!=null){
                                com+="<li class='ulli'><div class='lidiv'>" +
                                    "<div class='lidiv1'><div class='liImgdiv'><img class='UserImg' src='"+res.obj[i].userImg+"'/></div></div>" +
                                    "<div class='licondiv'><p class='userNane'>"+res.obj[i].userName+"</p>" +
                                    "<p class='comp'>"+res.obj[i].content+"</p>" +
                                    "<p class='actp'>"+settime(res.obj[i].commentDate)+"" +
                                    "<span class='pspupd' onclick='updcom("+res.obj[i].id+")'>隐藏</span>" +
                                    "<span class='pspdel' onclick='delcom("+res.obj[i].id+")'>删除</span></p></div>" +
                                    "<input class='comid' value='"+res.obj[i].id+"' type='hidden' name='comid'></div></li>";
                            }else {
                                com+="<li class='ulli'><div class='lidiv'>" +
                                    "<div class='lidiv1'><div class='liImgdiv'><i class='layui-icon' class='li_i'>&#xe612;</i></div></div>" +
                                    "<div class='licondiv'><p class='userNane'>"+res.obj[i].userName+"</p>" +
                                    "<p class='comp'>"+res.obj[i].content+"</p>" +
                                    "<p class='actp'>"+settime(res.obj[i].commentDate)+
                                    "<span class='pspupd' onclick='updcom("+res.obj[i].id+")'>隐藏</span>" +
                                    "<span class='pspdel' onclick='delcom("+res.obj[i].id+")'>删除</span></p></div>" +
                                    "<input class='comid' value='"+res.obj[i].id+"' type='hidden' name='comid'></div></li>";
                            }
                        }
                        $("#commentUl").html(com);
                    }else{
                        $('#nocomDiv').css("display","block");
                        $('#adiv').css("display","none");
                    }
                } else {
                    $('#nocomDiv').css("display","block");
                    $('#adiv').css("display","none");
                }
            });
        }

        //加载更多评论
        $("#getmore").on("click",function () {
            var num2=limi+3;
            getcomment(num2);
        });

        /*日期转换*/
        function settime(da){
            var date=new Date(da);
            var year=date.getFullYear();
            var month=date.getMonth()+1;
            var day=date.getDate();
            var hours=date.getHours();
            var mm=date.getMinutes();
            var s=date.getSeconds();
            return year+'-'+add(month)+'-'+add(day)+' '+add(hours)+':'+add(mm)+':'+add(s);
        };
        function add(time){//给非整数添0
            return time<10?'0'+time:time;
        };

        //提交评论—回复
        $("#subBtn").on("click",function () {
            var uId=$("#UserId").val();
            var cont=$("#textsId").val();
            var mcId=$("#mcId").val();
            if(cont){
                $.ajax({
                    url:"/tea/addComment",
                    type:"post",
                    dataType:"json",
                    contentType:"application/json",
                    data:JSON.stringify({"uId":uId,"id":mcId,"cont":cont,"type":3,"pId":0}),
                    success:function(data){
                        if (data.code>-1) {
                            layer.msg("评论成功", {icon: 1}, function () {
                                location.reload();
                            });
                        } else {
                            layer.msg("数据加载失败", {icon: 2});
                        }
                    },
                    error: function () {
                        layer.msg('数据获取失败！');
                    }
                });
            }else{
                layer.msg("先写点什么吧~");
            }
        });

        //刷新按钮
        $("#breakbtn").on("click",function () {
            location.reload();//刷新页面
        });
        //返回按钮
        $("#returnbtn").on("click",function () {
            history.go(-1);//数据全部还在 //history.back(-1)//直接返回当前页的上一页，是个新页面
        });
    });

//隐藏评论
function updcom(id){
    var uId=$("#UserId").val();
    $.post("/tea/updComment", {"id": id,"uId":uId}, function (res) {
        if (res.code>-1) {
            layer.msg("操作成功", {icon: 1},function(){
                location.reload();//刷新页面
            });
        } else {
            layer.msg("数据加载失败", {icon: 2});
        }
    });
};
//删除评论
function delcom(id) {
    $.post("/tea/delComment", {"id": id}, function (res) {
        if (res.code>-1) {
            layer.msg("删除成功", {icon: 1},function(){
                location.reload();//刷新页面
            });
        } else {
            layer.msg("数据加载失败", {icon: 2});
        }
    });
};